﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.PR.PurchaseRequest;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForItemToPrPrice : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //请购单
            UFIDA.U9.PR.PurchaseRequest.PR pr = key.GetEntity() as UFIDA.U9.PR.PurchaseRequest.PR;
            if (pr == null) return;

            #region 请购单新增时，当选中料品时,判断请购单私有字段1是否为空
            //判断请购单状态(开立,核准中)
            if (pr.Status == PRStatusEnum.OpenOpen || pr.Status == PRStatusEnum.Approving)
            {
                String sql = $@"SELECT  max(Code) Code from  Base_User where Name ='{pr.CreatedBy}' GROUP BY Name ";
                DataSet ds = null;
                DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                if (ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count == 0 || ds.Tables[0].Rows[0] == null)
                {
                    return;
                }
                else {
                    pr.DescFlexField.PrivateDescSeg6 = ds.Tables[0].Rows[0][0].ToString();
                }
                //foreach (var line in pr.PRLineList)
                //{
                //    ItemMaster item = line.ItemInfo.ItemID;
                //    decimal reqQtyTU = line.ReqQtyTU;
                //    //判断预估单价为空
                //    if (line.DescFlexSegments.PrivateDescSeg1 == "" || line.DescFlexSegments.PrivateDescSeg1 == null)
                //    {
                //        if (item.DescFlexField.PrivateDescSeg4 == "" || item.DescFlexField.PrivateDescSeg4 == null)
                //        {
                //            throw new Exception("请输入预估单价");
                //        }
                //        else
                //        {
                //            line.DescFlexSegments.PrivateDescSeg1 = item.DescFlexField.PrivateDescSeg4;
                //            decimal price = decimal.Parse(line.DescFlexSegments.PrivateDescSeg1);
                //            line.DescFlexSegments.PrivateDescSeg2 = (price * reqQtyTU).ToString();
                //        }
                //    }
                //    else
                //    {
                //        decimal price = decimal.Parse(line.DescFlexSegments.PrivateDescSeg1);
                //        line.DescFlexSegments.PrivateDescSeg2 = (price * reqQtyTU).ToString();
                //    }
                //}


                #endregion

            }
        }

    }
}
